﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class backmanage_API_A_Email : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        string returnStr = string.Empty;
        string editType = string.IsNullOrEmpty(Request.Form["edit"]) ? "" : Request.Form["edit"];
        switch (editType)
        {
            case "smtpadd":
                {
                    returnStr = SmtpAdd();
                    break;
                }
            case "smtplist":
                {
                    returnStr = SmtpList();
                    break;
                }
            case "smtpinfo":
                {
                    returnStr=SmtpInfo();
                    break;
                }
            case "smtpupdate":
                {
                    returnStr = SmtpUpdate();
                    break;
                }
            case "smtpdelete":
                {
                    returnStr = SmtpDelete();
                    break;
                }
            default:
                {
                    break;
                }
        }
        Response.Write(returnStr);
    }
    protected string SmtpAdd()
    {
        string returnStr = string.Empty;
        string address = string.IsNullOrEmpty(Request.Form["address"]) ? "" : Request.Form["address"];
        string port = string.IsNullOrEmpty(Request.Form["port"]) ? "" : Request.Form["port"];
        string username = string.IsNullOrEmpty(Request.Form["username"]) ? "" : Request.Form["username"];
        string password = string.IsNullOrEmpty(Request.Form["password"]) ? "" : Request.Form["password"];
        if (!address.Equals("") && !port.Equals("") && !username.Equals("") && !password.Equals(""))
        {
            MODEL.EmailSmtp model = new MODEL.EmailSmtp();
            model.ESMTPADDRESS = address;
            model.EPORT = Convert.ToInt32(port);
            model.EUSERNAME = username;
            model.EPASSWORD = password;
            model.ECREATEID = "admin";
            bool flag = new BLL.EmailSmtp().EmailSmtpAdd(model);
            if (flag)
                returnStr = "true";
            else
                returnStr = "false";
        }
        else
        {
            returnStr = "false";
        }
        return returnStr;
    }
    protected string SmtpUpdate()
    {
        string returnStr = string.Empty;
        string address = string.IsNullOrEmpty(Request.Form["address"]) ? "" : Request.Form["address"];
        string port = string.IsNullOrEmpty(Request.Form["port"]) ? "" : Request.Form["port"];
        string username = string.IsNullOrEmpty(Request.Form["username"]) ? "" : Request.Form["username"];
        string password = string.IsNullOrEmpty(Request.Form["password"]) ? "" : Request.Form["password"];
        int id = string.IsNullOrEmpty(Request.Form["id"]) ? 0 : Convert.ToInt32(Request.Form["id"]);
        if (!address.Equals("") && !port.Equals("") && !username.Equals("") && !password.Equals("")&&!id.Equals(0))
        {
            MODEL.EmailSmtp model = new MODEL.EmailSmtp();
            model.ID = id;
            model.ESMTPADDRESS = address;
            model.EPORT = Convert.ToInt32(port);
            model.EUSERNAME = username;
            model.EPASSWORD = password;
            bool flag = new BLL.EmailSmtp().EmailSmtpUpdate(model);
            if (flag)
                returnStr = "true";
            else
                returnStr = "false";
        }
        else
        {
            returnStr = "false";
        }
        return returnStr;
    }
    protected string SmtpList()
    {
        StringBuilder returnStr = new StringBuilder();
        DataSet ds = new BLL.EmailSmtp().EmailSmtpList();
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    returnStr.Append("<tr id=\""+dr["Eid"]+"\">");
                    returnStr.Append("<td>"+dr["Esmtpaddress"]+"</td>");
                    returnStr.Append("<td>"+dr["Eport"]+"</td>");
                    returnStr.Append("<td>"+dr["Eusername"]+"</td>");
                    returnStr.Append("<td>"+dr["Ecreateid"]+"</td>");
                    returnStr.Append("<td><a href=\"javascript:void(0)\" title=\"update\">修改</a><a href=\"javascript:void(0)\" title=\"delete\">删除</a></td>");
                    returnStr.Append("</tr>");
                }
            }
        }
        return returnStr.ToString();
    }
    protected string SmtpInfo()
    {
        StringBuilder returnStr = new StringBuilder();
        int id = Convert.ToInt32(string.IsNullOrEmpty(Request.Form["id"]) ? "0" : Request.Form["id"]);
        if (!id.Equals(0))
        {
            DataSet ds = new BLL.EmailSmtp().EmailSmtpList(id);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    returnStr.Append("{");
                    returnStr.Append("\"address\":\"" + ds.Tables[0].Rows[0]["Esmtpaddress"] + "\",");
                    returnStr.Append("\"port\":\"" + ds.Tables[0].Rows[0]["Eport"] + "\",");
                    returnStr.Append("\"username\":\"" + ds.Tables[0].Rows[0]["Eusername"] + "\",");
                    returnStr.Append("\"password\":\"" + ds.Tables[0].Rows[0]["Epassword"] + "\"");
                    returnStr.Append("}");
                }
            }
        }
        return returnStr.ToString();
    }
    protected string SmtpDelete()
    {
        string returnStr = string.Empty;
        int id = string.IsNullOrEmpty(Request.Form["id"]) ? 0 : Convert.ToInt32(Request.Form["id"]);
        if (!id.Equals(0))
        {
            bool flag = new BLL.EmailSmtp().EmailSmtpDelete(id);
            if (flag)
                returnStr = "true";
            else
                returnStr = "false";
        }
        else
        {
            returnStr = "false";
        }
        return returnStr;
    }
}
